import random


class Bargain:
    def __init__(self):
        self.MINMONEY = 1
        self.MAXMONEY = 10 * 10000

        self.TIMES = 3.1

    def isRight(self, money, surplusKnife):
        avg = money / surplusKnife
        if avg < self.MINMONEY or avg > self.MAXMONEY:
            return False
        return True

    def randomReducePrice(self, money, minS, maxS, count):
        '''
        :param money:
        :param minS:
        :param maxS:
        :param count:
        :return:
        '''
        if count == 1:
            return money
        if minS == maxS:
            return minS
        max = money if maxS > money else maxS
        maxY = money - (count - 1) * minS
        minY = money - (count - 1) * maxS
        min = minS if minS > minY else minY
        max = maxY if max > maxY else max

        return int(random.random() * (max - min) + min)

    def splitReducePrice(self, money, count):
        print(money, count)
        price_arr = []
        if not self.isRight(money, count):
            return price_arr
        # 每个砍价的最大金额为平均金额的TIMES倍
        max = (money * self.TIMES / count)
        max = self.MAXMONEY if max > self.MAXMONEY else max

        sum = 0
        for i in range(count):
            one = self.randomReducePrice(money, self.MINMONEY, max, count - i)
            price_arr.append(one / 100)
            money -= one
            sum += one
        return price_arr


def redbags(money, num=10):
    import random
    choice = random.sample(range(1, int(money * 100)), num - 1)
    print(choice)
    choice.extend([0,money*100])
    print(choice)
    choice.sort()
    print(choice)
    return [(choice[i + 1] - choice[i]) / 100 for i in range(num)]

def hb_generator(hongbao_num, money_sum):
    hongbao_remain = hongbao_num
    for i in range(hongbao_num - 1):
        min = 0.01
        max = money_sum / hongbao_remain * 2
        money = round(random.uniform(0, max), 2)
        hongbao_money = money if money > min else min
        hongbao_remain -= 1
        money_sum -= hongbao_money
        yield hongbao_money
    yield round(money_sum, 2)


if __name__ == '__main__':
    bargain = Bargain()
    print(bargain.splitReducePrice(5099, 10))
    print(bargain.splitReducePrice(5099, 10))
    num = 0
    xx = bargain.splitReducePrice(5099, 10)
    for i in xx:
        num += i
    print(num)

    print(redbags(50.99))
    print('---')
    xx = ','.join([str(num) for num in hb_generator(13,145.00)])
    print(xx)
    # print(xx.pop())
    # print(xx)
